package com.dtda.edu.miaosha.common.exception;

import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.dtda.edu.miaosha.common.response.EmBusinessError;
import com.dtda.edu.miaosha.common.response.ResponseType;

/**
 * @author 袁强
 * @version 2019年1月29日 下午5:32:11
 * 类说明  
 */
public class BaseExcpController {

	//异常处理器
	@ExceptionHandler(Exception.class)   //当接受一个什么样的异常(异常类)执行此异常处理器,这里是异常根类exception
	@ResponseStatus(HttpStatus.OK)    //保证页面请求一直为 200
	@ResponseBody   //保证返回json格式
	public Object handlerException(HttpServletRequest request,Exception e) {
		
		//ResponseType responseType = new ResponseType();
		//responseType.setStatus("fail");
		HashMap<Object,Object> responseData = new HashMap<>();
		//responseType.setData(responseData);
		
		if(e instanceof BusinessException) {
			
			BusinessException businessException= (BusinessException) e;
			
			responseData.put("errCode", businessException.getErrCode());
			responseData.put("errMsg", businessException.getErrMsg());
			
		}else {
			
			responseData.put("errCode", EmBusinessError.UNKNOWN_ERROR.getErrCode());
			responseData.put("errMsg", EmBusinessError.UNKNOWN_ERROR.getErrMsg());
		}
		
		
		return ResponseType.create("fail", responseData);
	}
}
